<?php
/**
* This page is used to edit a page's text and/or settings.
* It can will not create a new page, but will redirect a page to /new if one is not found.
*
* @package folio
* @param string $page The page title being edited.  Required.
* @param string $user The user/community to whom this page belongs.
**/

    require_once("../../../includes.php");

    define("context", "folio_page_edit");
    global $CFG;
    global $metatags;
    global $function;

    require_once("../view/tags.php");
    require_once("../view/resource.php");
    require_once("../view/page.php");
    require_once("../view/PageEditor.php");
    require_once("../view/artifact.php");

    require_once("../view/decoration.php");
    require_once("../view/file.php");

    require_once("../redirect.php");
    require_once("../model/user.php");
    require_once("../model/page_permission.php");

    // Include the tinymce editor.
    global $parameter;
    $parameter = array( array('body'));

    // Reset the function editor path.  This needs to be set for some reason,
    //        somehow it's lost in the Elgg code.  I tested, and it has nothing to
    //        do with my require_once statements ahead -- go figure.
    $function['userdetails:editor'][] = $CFG->dirroot . "mod/tinymce/tinymce_userdetails.php";


    // Retrieve page name & user name.
    $page_title = folio_page::title_decode( required_param('page') );
    $username = folio_clean(required_param('user'));

    $title = __gettext("Edit ") . ' "' . $page_title . '"';

    $user = folio_user::SelectWhereUsername( $username );
    if ( !$user )
        trigger_error('Invalid username ' . $username . ' passed to edit.php.', E_USER_ERROR);

    // set the current user.
    global $page_owner;
    $page_owner = $user->ident;

    // Get the $page_ident where page & user matches.
    $page = folio_page::SelectWhereTitleAndUsername($page_title,$username);

    // Load form helper class
    $form = new folio_BaseView();

    // If this page doesn't exist, redirect.
    if( !$page ) {
        header("Location: {$CFG->wwwroot}{$username}/page/" .
               folio_page::title_encode($page_title) . "/new");
        exit();
    }

    if ( !$page->Permission( 'update' ) ) {

        // User doesn't have permission to write the page.
        $body = __gettext('You do not have permission to edit this page.');
        $function['display:sidebar'] = array();

    } else {
        // Load Edit Screen

        //require_once( $CFG->dirroot . "mod/tinymce/tinymce_js.php");
        //$body = folio_page::WikiEditor($page);
        //$title = __gettext("Edit Page");
        //$page_title = $title;

        // Add wiki editor sidebar
        //sidebar_add(99,'folio_wikieditor', folio_page::WikiEditor_sidebar($page) );

        $form->Load( new folio_PageEditor($page) );

        $title =  "<a href='{$CFG->wwwroot}page/{$page->page_ident}'>" .
            "<img src='{$CFG->wwwroot}mod/folio/image/go-previous.png' alt='" .
            __gettext('Back to Page') . "' border=0/>" .
            "</a>";
        $page_title = __gettext('Edit Page');

        // Add rich editor sidebar
        $sidebar_form = new folio_TagLinkViewer($page);

        sidebar_add( 99, 'folio_richeditor', $sidebar_form->toString() );

        // Build the html controls
        $body = $form->toString() . '<br/><br/>' . $title;
		
		// Create the log entry
		folio_history_pagelog($page, 'edit' );
    }

    // Clear out sidebar
    //  If you have plug-ins that add things to the sidebar, you'll need to add them.
    //        to the config.php file.
    sidebar_remove($FOLIO_CFG->sidebar_title_list);
    sidebar_remove('folio_content');

    header("Cache-control: private");
    $body = templates_draw(array(
                    'context' => 'contentholder',
                    'title' => $title,
                    'body' => $body
                ));

    echo templates_page_draw(array(
                    $page_title, $body
                ));

?>
